(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.sysNote.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'sysNoteService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, sysNoteService) {
		var ctrl = this, sysNoteInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.sysNote = {};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.noteMsgsCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			ctrl.innerGridOptions.data = 'vm.sysNote.noteMsgs';
			authService.delete(sysNoteInfo).then(function(response) {
				ctrl.sysNote = response.data || sysNoteInfo;
				ctrl.sysNote.noteMsgs = ctrl.sysNote.noteMsgs || [];
				ctrl.localOptions = response.data.model.localList;
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedMessages = selectedRows;
		}
		
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.sysNote.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.sysNote).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.sysNote.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sysnote.list',
					'state' : 'cda.sysNote.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);